home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / WFC010.ZIP / SRC / CNETCONN.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-07  |  7.4 KB  |  304 lines

  1. #include <wfc.h>
  2. #pragma hdrstop
  3.  
  4. /*
  5. ** Author: Samuel R. Blackburn
  6. ** CI$: 76300,326
  7. ** Internet: sammy@sed.csc.com
  8. **
  9. ** You can use it any way you like as long as you don't try to sell it.
  10. **
  11. ** Any attempt to sell WFC in source code form must have the permission
  12. ** of the original author. You can produce commercial executables with
  13. ** WFC but you can't sell WFC.
  14. **
  15. ** Copyright, 1995, Samuel R. Blackburn
  16. **
  17. ** $Workfile: $
  18. ** $Revision: $
  19. ** $Modtime: $
  20. */
  21.  
  22. #if defined( _DEBUG )
  23. #undef THIS_FILE
  24. static char BASED_CODE THIS_FILE[] = __FILE__;
  25. #endif
  26.  
  27. IMPLEMENT_SERIAL( CNetworkConnectionInformation, CObject, 1 )
  28. IMPLEMENT_SERIAL( CNetworkConnections, CNetwork, 1 )
  29.  
  30. #if defined( _DEBUG )
  31. #define new DEBUG_NEW
  32. #endif
  33.  
  34. /*
  35. ** CNetworkConnectionInformation stuff
  36. */
  37.  
  38. CNetworkConnectionInformation::CNetworkConnectionInformation()
  39. {
  40.    m_Initialize();
  41. }
  42.  
  43. CNetworkConnectionInformation::CNetworkConnectionInformation( CONNECTION_INFO_1 *source )
  44. {
  45.    Copy( source );
  46. }
  47.  
  48. CNetworkConnectionInformation::CNetworkConnectionInformation( const CNetworkConnectionInformation& source )
  49. {
  50.    Copy( source );
  51. }
  52.  
  53. CNetworkConnectionInformation::~CNetworkConnectionInformation()
  54. {
  55.    m_Initialize();
  56. }
  57.  
  58. void CNetworkConnectionInformation::Copy( CONNECTION_INFO_1 *source )
  59. {
  60.    ASSERT( source != NULL );
  61.  
  62.    if ( source == NULL )
  63.    {
  64.       m_Initialize();
  65.       return;
  66.    }
  67.  
  68. #if ! defined( UNICODE )
  69.    ::UNICODE_to_ASCII( (LPCWSTR) source->coni1_username, source->coni1_username );
  70.    ::UNICODE_to_ASCII( (LPCWSTR) source->coni1_netname,  source->coni1_netname  );
  71. #endif
  72.  
  73.    ID            = source->coni1_id;
  74.    Type          = source->coni1_type;
  75.    NumberOfOpens = source->coni1_num_opens;
  76.    NumberOfUsers = source->coni1_num_users;
  77.    Time          = source->coni1_time;
  78.    UserName      = source->coni1_username;
  79.    NetName       = source->coni1_netname;
  80.  
  81. #if ! defined( UNICODE )
  82.    ::ASCII_to_UNICODE( source->coni1_username, (LPWSTR) source->coni1_username );
  83.    ::ASCII_to_UNICODE( source->coni1_netname,  (LPWSTR) source->coni1_netname  );
  84. #endif
  85. }
  86.  
  87. void CNetworkConnectionInformation::Copy( const CNetworkConnectionInformation& source )
  88. {
  89.    ASSERT( this != &source );
  90.  
  91.    /*
  92.    ** Check to make sure we ain't copying ourselves
  93.    */
  94.  
  95.    if ( this == &source )
  96.    {
  97.       return;
  98.    }
  99.  
  100.    ID            = source.ID;
  101.    Type          = source.Type;
  102.    NumberOfOpens = source.NumberOfOpens;
  103.    NumberOfUsers = source.NumberOfUsers;
  104.    Time          = source.Time;
  105.    UserName      = source.UserName;
  106.    NetName       = source.NetName;
  107. }
  108.  
  109. #if defined( _DEBUG )
  110.  
  111. void CNetworkConnectionInformation::Dump( CDumpContext& dump_context ) const
  112. {
  113.    CObject::Dump( dump_context );
  114.  
  115.    dump_context << "ID = "            << ID            << "\n";
  116.    dump_context << "Type = "          << Type          << "\n";
  117.    dump_context << "NumberOfOpens = " << NumberOfOpens << "\n";
  118.    dump_context << "NumberOfUsers = " << NumberOfUsers << "\n";
  119.    dump_context << "Time = "          << Time          << "\n";
  120.    dump_context << "UserName = \""    << UserName      << "\"\n";
  121.    dump_context << "NetName = \""     << NetName       << "\"\n";
  122. }
  123.  
  124. #endif // _DEBUG
  125.  
  126. void CNetworkConnectionInformation::Empty( void )
  127. {
  128.    m_Initialize();
  129. }
  130.  
  131. void CNetworkConnectionInformation::m_Initialize( void )
  132. {
  133.    UserName.Empty();
  134.    NetName.Empty();
  135.    ID            = 0;
  136.    Type          = 0;
  137.    NumberOfUsers = 0;
  138.    NumberOfOpens = 0;
  139.    Time          = 0;
  140. }
  141.  
  142. void CNetworkConnectionInformation::Serialize( CArchive& archive )
  143. {
  144.    CObject::Serialize( archive );
  145.  
  146.    if ( archive.IsStoring() )
  147.    {
  148.       archive << ID;
  149.       archive << Type;
  150.       archive << NumberOfOpens;
  151.       archive << NumberOfUsers;
  152.       archive << UserName;
  153.       archive << NetName;
  154.    }
  155.    else
  156.    {
  157.       archive >> ID;
  158.       archive >> Type;
  159.       archive >> NumberOfOpens;
  160.       archive >> NumberOfUsers;
  161.       archive >> UserName;
  162.       archive >> NetName;
  163.    }
  164. }
  165.  
  166. const CNetworkConnectionInformation& CNetworkConnectionInformation::operator=( const CNetworkConnectionInformation& source )
  167. {
  168.    ASSERT( this != &source );
  169.  
  170.    if ( this != &source )
  171.    {
  172.       Copy( source );
  173.    }
  174.  
  175.    return( *this );
  176. }
  177.  
  178. /*
  179. ** CNetworkConnections Stuff
  180. */
  181.  
  182. CNetworkConnections::CNetworkConnections()
  183. {
  184.    m_Initialize();
  185. }
  186.  
  187. CNetworkConnections::CNetworkConnections( LPCTSTR machine_name )
  188. {
  189.    m_Initialize();
  190.    Open( machine_name );
  191. }
  192.  
  193. CNetworkConnections::~CNetworkConnections()
  194. {
  195.    Close();
  196.    m_Initialize();
  197. }
  198.  
  199. void CNetworkConnections::Close( void )
  200. {
  201.    CNetwork::Close();
  202.  
  203.    if ( m_1InformationBuffer != NULL )
  204.    {
  205.       ::NetApiBufferFree( m_1InformationBuffer );
  206.       m_1InformationBuffer = NULL;
  207.    }
  208. }
  209.  
  210. #if defined( _DEBUG )
  211.  
  212. void CNetworkConnections::Dump( CDumpContext& dump_context ) const
  213. {
  214.    CNetwork::Dump( dump_context );
  215.  
  216.    dump_context << "m_1ResumeHandle = "         << m_1ResumeHandle         << "\n";
  217.    dump_context << "m_1CurrentEntryNumber = "   << m_1CurrentEntryNumber   << "\n";
  218.    dump_context << "m_1NumberOfEntriesRead = "  << m_1NumberOfEntriesRead  << "\n";
  219.    dump_context << "m_1TotalNumberOfEntries = " << m_1TotalNumberOfEntries << "\n";
  220. }
  221.  
  222. #endif // _DEBUG
  223.  
  224. BOOL CNetworkConnections::Enumerate( LPCTSTR share_or_computer_name )
  225. {
  226.    if ( m_1InformationBuffer != NULL )
  227.    {
  228.       ::NetApiBufferFree( m_1InformationBuffer );
  229.       m_1InformationBuffer = NULL;
  230.    }
  231.  
  232.    if ( share_or_computer_name == NULL )
  233.    {
  234.       m_ErrorCode = ERROR_INVALID_PARAMETER;
  235.       return( FALSE );
  236.    }
  237.  
  238.    LPWSTR wide_share_or_computer_name[ MAX_PATH ];
  239.  
  240. #if ! defined( UNICODE )
  241.    ASCII_to_UNICODE( share_or_computer_name, (LPWSTR) wide_share_or_computer_name );
  242. #else
  243.    strcpy( wide_share_or_computer_name, share_or_computer_name );
  244. #endif // UNICODE
  245.  
  246.    m_1CurrentEntryNumber   = 0;
  247.    m_1NumberOfEntriesRead  = 0;
  248.    m_1ResumeHandle         = 0;
  249.    m_1TotalNumberOfEntries = 0;
  250.  
  251.    m_ErrorCode = ::NetConnectionEnum( (LPTSTR) m_WideMachineName,
  252.                                       (LPTSTR) wide_share_or_computer_name, 
  253.                                                1, 
  254.                                    (LPBYTE *) &m_1InformationBuffer,
  255.                                                65535,
  256.                                               &m_1NumberOfEntriesRead,
  257.                                               &m_1TotalNumberOfEntries,
  258.                                               &m_1ResumeHandle );
  259.  
  260.    if ( m_ErrorCode != NERR_Success || m_1InformationBuffer == NULL )
  261.    {
  262.       return( FALSE );
  263.    }
  264.  
  265.    return( TRUE );
  266. }
  267.  
  268. BOOL CNetworkConnections::GetNext( CNetworkConnectionInformation& information )
  269. {
  270.    if ( m_1CurrentEntryNumber < m_1TotalNumberOfEntries )
  271.    {
  272.       information.Copy( &m_1InformationBuffer[ m_1CurrentEntryNumber ] );
  273.       m_1CurrentEntryNumber++;
  274.       return( TRUE );
  275.    }
  276.  
  277.    information.Empty();
  278.    return( FALSE );
  279. }
  280.  
  281. void CNetworkConnections::m_Initialize( void )
  282. {
  283.    m_ErrorCode               = 0;
  284.    m_1InformationBuffer    = NULL;
  285.    m_1ResumeHandle         = 0;
  286.    m_1CurrentEntryNumber   = 0;
  287.    m_1NumberOfEntriesRead  = 0;
  288.    m_1TotalNumberOfEntries = 0;
  289. }
  290.  
  291. void CNetworkConnections::Serialize( CArchive& archive )
  292. {
  293.    CNetwork::Serialize( archive );
  294.  
  295.    if ( archive.IsStoring() )
  296.    {
  297.       archive << m_ErrorCode;
  298.    }
  299.    else
  300.    {
  301.       archive >> m_ErrorCode;
  302.    }
  303. }
  304.